/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableIPAddress;
import neptune.web.client.model.topology.SerializableNetworkInterface;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class AddIpAddrCommand implements Command {

	private SerializableNetworkInterface netIf;
	private TreeItem item;

	public AddIpAddrCommand(SerializableNetworkInterface netIF, TreeItem item) {
		this.netIf = netIF;
		this.item = item;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {

		if (netIf.getIpAddresses().size() >= 1) {
			Window.alert("You can define just 1"
					+ " IP address for interface: "
					+ "Only OLPI link multiplexing tech "
					+ "is supported right now!");
			return;
		}

		final SerializableIPAddress addr = new SerializableIPAddress();
		addr.setAddress("UNDEFINED");
		netIf.getIpAddresses().add(addr);

		final TreeItem addrItem = new TreeItem();
		addrItem.setWidget(new IpAddressMenuBar(addr, netIf, addrItem));
		item.addItem(addrItem);

		IpAddrPropsDialog dialog = new IpAddrPropsDialog(addr, addrItem);
		dialog.setCancelCallback(new CancelCallBack() {
			public void onCancel() {
				addrItem.remove();
				netIf.getIpAddresses().remove(addr);
			}
		});
		dialog.center();
	}

}
